<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <TreeTools :node-data="companyInfo" :is-root="true" @add="addDepartment" />

        <hr>
        <!-- 树型结构 -->
        <el-tree
          :data="depts"
          :props="{label: 'name'}"
        >
          <!-- 插槽 -->
          <template #default="obj">

            <TreeTools
              :node-data="obj.data"
              @del="getDepartment"
              @add="addDepartment"
              @edit="editDepartment"
            />
          </template>

        </el-tree>
      </el-card>
      <!-- 点击弹出框 -->
      <AddDept
        ref="addDept"
        :is-show-dialog.sync="isShowDialog"
        :node-data="nodeData"
        @update="getDepartment"
      />
    </div>
  </div>
</template>

<script>
// 引入组件
import AddDept from './components/add-departments.vue'
import TreeTools from './components/terr-tools.vue'
import { Department } from '@/api/departments'
import { listToTreeData } from '@/utils/validate'
export default {
  // 注册
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      companyInfo: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      depts: [], // 部门列表
      isShowDialog: false, // 对话框默认隐藏
      nodeData: {} // 被点击的数据
    }
  },
  async created() {
    await this.getDepartment()
  },
  methods: {

    async  getDepartment() {
      // 发送请求拿数据
      const { depts } = await Department()
      // console.log(depts)
      // 赋值给变量
      this.depts = listToTreeData(depts, '')
    },

    // 点击添加
    addDepartment(nodeData) {
      // 显示会话框
      // console.log(456)
      this.isShowDialog = true
      this.nodeData = nodeData
    },
    async editDepartment(nodeData) {
      // console.log('编辑弹窗')
      // 显示弹窗
      this.isShowDialog = true
      await this.$refs.addDept.getDepartment(nodeData.id)
      // 传进来的部门
      this.nodeData = nodeData
      // console.log(89, this.nodeData)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
